package u.can.i.up.gearmanimpl;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.gearman.Gearman;
import org.gearman.GearmanClient;
import org.gearman.GearmanJobEvent;
import org.gearman.GearmanJobReturn;
import org.gearman.GearmanServer;

import u.can.i.up.gearmanimpl.common.Configuration;
import u.can.i.up.gearmanimpl.common.Constants;

public class Client {
	
	/** The host address of the job server */
	public static final String SERVER_HOST =  Configuration.get(Constants.SERVERIP, "127.0.0.1");
	public static final int SERVER_PORT = Configuration.getInt(Constants.SERVERPORT, 4730);
	public static final String FUNCTION_NAME = "HTTP";
	
	static Logger logger = LogManager.getLogger(Worker.class);
	
	public static void main(String... args) throws InterruptedException {
		submitMission();
	}


	public static void submitMission() throws InterruptedException{
		/*
		 * Create a Gearman instance
		 */
		Gearman gearman = Gearman.createGearman();
		/*
		 * Create a new gearman client.
		 * 
		 * The client is used to submit requests the job server.
		 */
		GearmanClient client = gearman.createGearmanClient();
		/*
		 * Create the job server object. This call creates an object represents
		 * a remote job server.
		 * 
		 * Parameter 1: the host address of the job server.
		 * Parameter 2: the port number the job server is listening on.
		 * 
		 * A job server receives jobs from clients and distributes them to
		 * registered workers.
		 */
		GearmanServer server = gearman.createGearmanServer(SERVER_HOST, SERVER_PORT);
		/*
		 * Tell the client that it may connect to this server when submitting
		 * jobs.
		 */
		client.addServer(server);
		/*
		 * Submit a job to a job server.
		 * 
		 * Parameter 1: the gearman function name
		 * Parameter 2: the data passed to the server and worker
		 * 
		 * The GearmanJobReturn is used to poll the job's result
		 */
//		String apkName = new String(args[0]);
		GearmanJobReturn jobReturn = client.submitJob(FUNCTION_NAME, "".toString().getBytes());
		while (!jobReturn.isEOF()) {

			// Poll the next job event (blocking operation)
			GearmanJobEvent event = jobReturn.poll();

			switch (event.getEventType()) {

			// success
			case GEARMAN_JOB_SUCCESS: // Job completed successfully
				// print the result
//				byte[] bytes = event.getData();
//				String sa = new String(bytes);
//				JSONObject json1 = JSONObject.fromObject(sa);
//				System.out.println(json1);
				logger.info("client");
				break;
				// failure
			case GEARMAN_SUBMIT_FAIL: // The job submit operation failed
			case GEARMAN_JOB_FAIL: // The job's execution failed
				System.err.println(event.getEventType() + ": "
						+ new String(event.getData()));
			default:
			}
		}
		/*
		 * Close the gearman service after it's no longer needed. (closes all
		 * sub-services, such as the client)
		 * 
		 * It's suggested that you reuse Gearman and GearmanClient instances
		 * rather recreating and closing new ones between submissions
		 */
		gearman.shutdown();
	}
}
